/**
 *
 * Created by Song,YeWen, 10/10/2011 6:02:13 PM
 *
 */

function validateVideoUrl(ori_url) 
{
    //We use a fake constant duration for v1.com, because it is News, not long duration video
    //TODO: FIXME
    var fcduration = '100'; 
    var html = download_page(ori_url);
    var matches = html.match(/{id:"(\d+)"/);
    var dst_url = '';
    var flv_str = '';
    if (!matches) {
    	 //case {id:"2011,10,7306"}
    	matches = html.match(/{id:"(\d+,\d+,\d+)"/);
    	if(!matches){
    		// vid not found
   	     	return 'NotFoundVideo';
    	}    	
    	dst_url = 'http://www.v1.cn/gqxml/' + matches[1].replace(/,/g,'/') + '.xml';    	
    	var xml_str = download_page(dst_url);
    	flv_str = parse_xml(xml_str, 'videoInfo', 'url');
    }else{
    	dst_url = 'http://www.v1.cn/xml/video/' + matches[1] + '.xml';    	
    	var xml_str = download_page(dst_url);
    	flv_str = parse_xml(xml_str, 'items', 'item', 'flv');
    }
    
    var sb = [];      
    sb.push(flv_str);
    if(sb.length){
       //matches = html.match(/<title>(.*?)<\/title>/);
       matches = html.match(/<title>([\s\S]*?)<\/title>/);
       if(matches){
    	   return 'success_title:' + encodeURIComponent(matches[1])
    	          + '\n video_urls:' + encodeURIComponent(sb)
    	          + '\n seconds:' + Math.round(parseInt(fcduration));
       }
    }
	return 'error:empty_path';
}

/* http://www.v1.cn/xml/video/720042.xml */
/* http://ent.v1.cn/xw/2011-9-17/1316144840510.shtml */
/* http://tv.v1.cn/ag/2011-10-18/1318907631762v.shtml */
function js_get_media_url(ori_url)
{		
    var html = download_page(ori_url);
    var matches = html.match(/{id:"(\d+)"/);
    var dst_url = '';
    var flv_str = '';
    if (!matches) {
        //case {id:"2011,10,7306"}
    	matches = html.match(/{id:"(\d+,\d+,\d+)"/);
    	if(!matches){
    		// Not Found   
           return '';
    	}    	
    	dst_url = 'http://www.v1.cn/gqxml/' + matches[1].replace(/,/g,'/') + '.xml';
    	var xml_str = download_page(dst_url);
    	flv_str = parse_xml(xml_str, 'videoInfo', 'url');
        return flv_str;
    }else{
    	dst_url = 'http://www.v1.cn/xml/video/' + matches[1] + '.xml';
    	var xml_str = download_page(dst_url);
        flv_str = parse_xml(xml_str, 'items', 'item', 'flv');
        return flv_str;
    }    
}